<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="介绍人ID" prop="postedMemberId">
        <el-input
          v-model="queryParams.postedMemberId"
          placeholder="请输入介绍人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务标题" prop="postedTitle">
        <el-input
          v-model="queryParams.postedTitle"
          placeholder="请输入任务标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属地区" prop="postedRegion">
        <el-input
          v-model="queryParams.postedRegion"
          placeholder="请输入所属地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务金额" prop="postedAmount">
        <el-input
          v-model="queryParams.postedAmount"
          placeholder="请输入任务金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金比例" prop="postedCommissionRate">
        <el-input
          v-model="queryParams.postedCommissionRate"
          placeholder="请输入佣金比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金金额" prop="postedCommissionAmount">
        <el-input
          v-model="queryParams.postedCommissionAmount"
          placeholder="请输入佣金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="要求完成时间" prop="postedRequiredTime">
        <el-date-picker clearable
          v-model="queryParams.postedRequiredTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择要求完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="承接人押金" prop="postedDeposit">
        <el-input
          v-model="queryParams.postedDeposit"
          placeholder="请输入承接人押金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示时间" prop="postedDisplayDuration">
        <el-input
          v-model="queryParams.postedDisplayDuration"
          placeholder="请输入显示时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布来源(0会员悬赏/1公众号发布)" prop="postedOrigin">
        <el-select v-model="queryParams.postedOrigin" placeholder="请选择发布来源(0会员悬赏/1公众号发布)" clearable>
          <el-option
            v-for="dict in dict.type.drp_posted_origin"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源id" prop="postedOriginId">
        <el-input
          v-model="queryParams.postedOriginId"
          placeholder="请输入来源id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="postedTime">
        <el-date-picker clearable
          v-model="queryParams.postedTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排序顺序" prop="sortOrder">
        <el-input
          v-model="queryParams.sortOrder"
          placeholder="请输入排序顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新人" prop="updatedBy">
        <el-input
          v-model="queryParams.updatedBy"
          placeholder="请输入最后更新人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reward:posted:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reward:posted:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reward:posted:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reward:posted:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="postedId" />
      <el-table-column label="介绍人ID" align="center" prop="postedMemberId" />
      <el-table-column label="任务标题" align="center" prop="postedTitle" />
      <el-table-column label="所属地区" align="center" prop="postedRegion" />
      <el-table-column label="任务金额" align="center" prop="postedAmount" />
      <el-table-column label="佣金比例" align="center" prop="postedCommissionRate" />
      <el-table-column label="佣金金额" align="center" prop="postedCommissionAmount" />
      <el-table-column label="要求完成时间" align="center" prop="postedRequiredTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.postedRequiredTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="承接人押金" align="center" prop="postedDeposit" />
      <el-table-column label="显示时间" align="center" prop="postedDisplayDuration" />
      <el-table-column label="任务内容" align="center" prop="postedDescription" />
      <el-table-column label="任务图片链接" align="center" prop="postedImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.postedImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发布来源(0会员悬赏/1公众号发布)" align="center" prop="postedOrigin">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.drp_posted_origin" :value="scope.row.postedOrigin"/>
        </template>
      </el-table-column>
      <el-table-column label="来源id" align="center" prop="postedOriginId" />
      <el-table-column label="发布时间" align="center" prop="postedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.postedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态：0待审核/1已审核/-1已拒绝/99已下架" align="center" prop="postedStatus" />
      <el-table-column label="排序顺序" align="center" prop="sortOrder" />
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新人" align="center" prop="updatedBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reward:posted:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reward:posted:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改悬赏任务管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="介绍人ID" prop="postedMemberId">
          <el-input v-model="form.postedMemberId" placeholder="请输入介绍人ID" />
        </el-form-item>
        <el-form-item label="任务标题" prop="postedTitle">
          <el-input v-model="form.postedTitle" placeholder="请输入任务标题" />
        </el-form-item>
        <el-form-item label="所属地区" prop="postedRegion">
          <el-input v-model="form.postedRegion" placeholder="请输入所属地区" />
        </el-form-item>
        <el-form-item label="任务金额" prop="postedAmount">
          <el-input v-model="form.postedAmount" placeholder="请输入任务金额" />
        </el-form-item>
        <el-form-item label="佣金比例" prop="postedCommissionRate">
          <el-input v-model="form.postedCommissionRate" placeholder="请输入佣金比例" />
        </el-form-item>
        <el-form-item label="佣金金额" prop="postedCommissionAmount">
          <el-input v-model="form.postedCommissionAmount" placeholder="请输入佣金金额" />
        </el-form-item>
        <el-form-item label="要求完成时间" prop="postedRequiredTime">
          <el-date-picker clearable
            v-model="form.postedRequiredTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择要求完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="承接人押金" prop="postedDeposit">
          <el-input v-model="form.postedDeposit" placeholder="请输入承接人押金" />
        </el-form-item>
        <el-form-item label="显示时间" prop="postedDisplayDuration">
          <el-input v-model="form.postedDisplayDuration" placeholder="请输入显示时间" />
        </el-form-item>
        <el-form-item label="任务内容" prop="postedDescription">
          <el-input v-model="form.postedDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务图片链接" prop="postedImage">
          <image-upload v-model="form.postedImage"/>
        </el-form-item>
        <el-form-item label="发布来源(0会员悬赏/1公众号发布)" prop="postedOrigin">
          <el-select v-model="form.postedOrigin" placeholder="请选择发布来源(0会员悬赏/1公众号发布)">
            <el-option
              v-for="dict in dict.type.drp_posted_origin"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源id" prop="postedOriginId">
          <el-input v-model="form.postedOriginId" placeholder="请输入来源id" />
        </el-form-item>
        <el-form-item label="发布时间" prop="postedTime">
          <el-date-picker clearable
            v-model="form.postedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排序顺序" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入排序顺序" />
        </el-form-item>
        <el-divider content-position="center">分销信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDrpDistr">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDrpDistr">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="drpDistrList" :row-class-name="rowDrpDistrIndex" @selection-change="handleDrpDistrSelectionChange" ref="drpDistr">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="直接介绍人ID (外键)" prop="distrPostedPid" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.distrPostedPid" placeholder="请输入直接介绍人ID (外键)" />
            </template>
          </el-table-column>
          <el-table-column label="上级介绍人ID (外键)" prop="distrUpperPostedPid" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.distrUpperPostedPid" placeholder="请输入上级介绍人ID (外键)" />
            </template>
          </el-table-column>
          <el-table-column label="分销金额" prop="distrAmount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.distrAmount" placeholder="请输入分销金额" />
            </template>
          </el-table-column>
          <el-table-column label="分销状态" prop="distrStatus" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.distrStatus" placeholder="请选择分销状态">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="贡献积分" prop="distrContributionPoints" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.distrContributionPoints" placeholder="请输入贡献积分" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.createdAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updatedAt" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.updatedAt" type="date" value-format="yyyy-MM-dd" placeholder="请选择更新时间" />
            </template>
          </el-table-column>
          <el-table-column label="最后更新人" prop="updatedBy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.updatedBy" placeholder="请输入最后更新人" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPosted, getPosted, delPosted, addPosted, updatePosted } from "@/api/reward/posted";

export default {
  name: "Posted",
  dicts: ['drp_posted_origin'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDrpDistr: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 悬赏任务管理表格数据
      postedList: [],
      // 分销信息表格数据
      drpDistrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postedMemberId: null,
        postedTitle: null,
        postedRegion: null,
        postedAmount: null,
        postedCommissionRate: null,
        postedCommissionAmount: null,
        postedRequiredTime: null,
        postedDeposit: null,
        postedDisplayDuration: null,
        postedDescription: null,
        postedImage: null,
        postedOrigin: null,
        postedOriginId: null,
        postedTime: null,
        postedStatus: null,
        sortOrder: null,
        createdAt: null,
        updatedAt: null,
        updatedBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postedMemberId: [
          { required: true, message: "介绍人ID不能为空", trigger: "blur" }
        ],
        postedTitle: [
          { required: true, message: "任务标题不能为空", trigger: "blur" }
        ],
        postedRegion: [
          { required: true, message: "所属地区不能为空", trigger: "blur" }
        ],
        postedAmount: [
          { required: true, message: "任务金额不能为空", trigger: "blur" }
        ],
        postedCommissionRate: [
          { required: true, message: "佣金比例不能为空", trigger: "blur" }
        ],
        postedCommissionAmount: [
          { required: true, message: "佣金金额不能为空", trigger: "blur" }
        ],
        postedRequiredTime: [
          { required: true, message: "要求完成时间不能为空", trigger: "blur" }
        ],
        postedDeposit: [
          { required: true, message: "承接人押金不能为空", trigger: "blur" }
        ],
        postedDisplayDuration: [
          { required: true, message: "显示时间不能为空", trigger: "blur" }
        ],
        postedImage: [
          { required: true, message: "任务图片链接不能为空", trigger: "blur" }
        ],
        postedOrigin: [
          { required: true, message: "发布来源(0会员悬赏/1公众号发布)不能为空", trigger: "change" }
        ],
        postedOriginId: [
          { required: true, message: "来源id不能为空", trigger: "blur" }
        ],
        postedTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        postedStatus: [
          { required: true, message: "任务状态：0待审核/1已审核/-1已拒绝/99已下架不能为空", trigger: "change" }
        ],
        sortOrder: [
          { required: true, message: "排序顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询悬赏任务管理列表 */
    getList() {
      this.loading = true;
      listPosted(this.queryParams).then(response => {
        this.postedList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postedId: null,
        postedMemberId: null,
        postedTitle: null,
        postedRegion: null,
        postedAmount: null,
        postedCommissionRate: null,
        postedCommissionAmount: null,
        postedRequiredTime: null,
        postedDeposit: null,
        postedDisplayDuration: null,
        postedDescription: null,
        postedImage: null,
        postedOrigin: null,
        postedOriginId: null,
        postedTime: null,
        postedStatus: null,
        sortOrder: null,
        createdAt: null,
        updatedAt: null,
        updatedBy: null
      };
      this.drpDistrList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postedId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加悬赏任务管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postedId = row.postedId || this.ids
      getPosted(postedId).then(response => {
        this.form = response.data;
        this.drpDistrList = response.data.drpDistrList;
        this.open = true;
        this.title = "修改悬赏任务管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.drpDistrList = this.drpDistrList;
          if (this.form.postedId != null) {
            updatePosted(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosted(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postedIds = row.postedId || this.ids;
      this.$modal.confirm('是否确认删除悬赏任务管理编号为"' + postedIds + '"的数据项？').then(function() {
        return delPosted(postedIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 分销信息序号 */
    rowDrpDistrIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 分销信息添加按钮操作 */
    handleAddDrpDistr() {
      let obj = {};
      obj.distrPostedPid = "";
      obj.distrUpperPostedPid = "";
      obj.distrAmount = "";
      obj.distrStatus = "";
      obj.distrContributionPoints = "";
      obj.createdAt = "";
      obj.updatedAt = "";
      obj.updatedBy = "";
      this.drpDistrList.push(obj);
    },
    /** 分销信息删除按钮操作 */
    handleDeleteDrpDistr() {
      if (this.checkedDrpDistr.length == 0) {
        this.$modal.msgError("请先选择要删除的分销信息数据");
      } else {
        const drpDistrList = this.drpDistrList;
        const checkedDrpDistr = this.checkedDrpDistr;
        this.drpDistrList = drpDistrList.filter(function(item) {
          return checkedDrpDistr.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDrpDistrSelectionChange(selection) {
      this.checkedDrpDistr = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reward/posted/export', {
        ...this.queryParams
      }, `posted_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
